﻿using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using NetTao.Model;

namespace NetTao.Analyser
{
    public class AnalyseChapter : AnalyseBase
    {
        public string ContentTxt { get; set; }

        private IList<AttachInfo> _attchs;
        public IList<AttachInfo> Attahcs
        {
            get { return _attchs; }
            set { if (value != null) _attchs = value; }
        }

        public void AnalyseChapterInfo(ChapterInfo chapter, ref string jsTxtLink, ref string encoding)
        {
            ContentTxt = "";
            var imagesLink = MatchSingleValue(Rule.PubContentImages);
            if (string.IsNullOrEmpty(imagesLink))
            {
                jsTxtLink = MatchSingleValue(Rule.PubTextUrl);
                encoding = Rule.PubTextUrl.FilterPattern;//js调用时，将编码写在filterpattern
                if (string.IsNullOrEmpty(jsTxtLink))//JSLINK不为空，则需要在外面将JS采集回来赋值给CONTENT
                {
                    ContentTxt = MatchSingleValue(Rule.PubContentText);
                    if (string.IsNullOrEmpty(ContentTxt))
                    {
                        throw new Exception("[10001]js txt is empty.");
                    } 
                    ClearTxtMark();
                }
            }
            else
            {
                AnalyseImage(chapter);
            }
        }

        public void ClearTxtMark()
        {
            if (string.IsNullOrEmpty(ContentTxt)) return;
            if (!ContentTxt.Contains("document.write"))
            {
                ContentTxt = "document.write(\"" + ContentTxt + "\");";
            }
            if (Rule.PubContentReplace.Method != "Replace") return;
            var arrs = Rule.PubContentReplace.Pattern.Split('♂');
            foreach (var arr in arrs)
            {
                ContentTxt = Regex.Replace(ContentTxt, arr, "", Rule.PubContentReplace.Options);
            }
        }

        //public string GetImagesLink()
        //{
        //    return MatchSingleValue(Rule.PubContentImages);
        //}

        //public string GetTextUrl()
        //{
        //    return MatchSingleValue(Rule.PubTextUrl);
        //}

        //private void AnalyseTxt()
        //{
        //    //如果为JS调用TXT，需要先将JS TXT路径匹配
        //    //采集该JS指向的文件，并赋值给content
        //    ContentTxt = MatchSingleValue(Rule.PubContentText);
        //    if(string.IsNullOrEmpty(ContentTxt))
        //    {
        //        throw new Exception("[10001]js txt is empty.");
        //    }
        //}

        private void AnalyseImage(ChapterInfo chapter)
        {
            if (_attchs == null) _attchs = new List<AttachInfo>();
            MatchCollection matchs = Regex.Matches(Content, Rule.PubContentImages.Pattern, Rule.PubContentImages.Options);
            for (int i = 0; i < matchs.Count; i++)
            {
                var match = matchs[i];
                var attach = new AttachInfo
                                 {
                                     articleid = Article.articleid,
                                     attachid = Config.AppCache.Instace().MaxAttachID,
                                     chapterid = chapter.chapterid,
                                     uptime = Config.AppCache.Instace().GetDateTime()
                                 };
                _attchs.Add(attach);//?有问题待解决，采集回来的未用上
            }
        }

    }
}
